package cn.lkk.samples.lang.inheritance.objectClass;

import com.sun.org.apache.bcel.internal.generic.INSTANCEOF;

import java.util.*;

public class Employee {
    private String name;
    private double salary;
    private Date hireDay;

    public Employee(String n, double s, int year, int month, int day) {
        name = n;
        salary = s;
        hireDay = new GregorianCalendar(year, month - 1, day).getTime();
    }

    public String getName() {
        return name;
    }

    public double getSalary() {
        return salary;
    }

    public Date getHireDay() {
        return hireDay;
    }

    public void raiseSalary(double byPercent) {
        double raise = salary * byPercent / 10;
        salary += raise;
    }

    /**
     * 比较两个对象是否相同
     *
     * @param obj 比较对象
     * @return 比较结果
     */
    @Override
    public boolean equals(Object obj) {
        //1. 快速比较
        if (obj == this)
            return true;

        //2. 检查是否为空
        if (obj == null)
            return false;

        //3. 检查是否类型兼容
        //if (getClass() != obj.getClass())
        if (!(obj instanceof Employee))
            return false;

        //4. 转换
        Employee other = (Employee) obj;

        //5. 比较
        return Objects.equals(name, other.name)
                && salary == other.salary
                && Objects.equals(hireDay, other.hireDay);
    }

    /**
     * 一般实现了 equals 方法之后，也实现 hashCode 方法
     */
    @Override
    public int hashCode() {
        //最便捷
        return Objects.hash(name, salary, hireDay);

        //较为便捷
        //return 7 * Objects.hashCode(name)
        //        + 11 * new Double(salary).hashCode()
        //        + 13 * Objects.hashCode(hireDay);

        //最原始
        //return 7 * name.hashCode()
        //        + 11 * new Double(salary).hashCode()
        //        + 13 * hireDay.hashCode();
    }

    @Override
    public String toString() {
        return getClass().getName()
                + "[name=" + getName()
                + ",salary=" + getSalary()
                + ",hireDay=" + hireDay
                + "]";
    }
}
